package com.keyancompany.zhongbaopingtai.weight;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.activity.CircleMemberActivity;
import com.keyancompany.zhongbaopingtai.api.CircleAPI;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.listener.IAsyncHttpResponseHandler;
import com.keyancompany.zhongbaopingtai.listener.LoadingHanlder;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.CircleModel;
import com.keyancompany.zhongbaopingtai.model.EventBusEntity;
import com.keyancompany.zhongbaopingtai.model.TeamUser;
import com.keyancompany.zhongbaopingtai.utils.IntentUtils;
import com.keyancompany.zhongbaopingtai.utils.ScreenUtils;
import com.keyancompany.zhongbaopingtai.utils.T;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by firefox on 2016/7/2.
 */
public class CircleOptionsPopup extends MenuPopuWindow implements IAsyncHttpResponseHandler {

    @Bind(R.id.exit_team)
    TextView exitTeam;
    @Bind(R.id.settlement_team)
    TextView settlementTeam;
    @Bind(R.id.leader_transfer)
    TextView leaderTransfer;
    @Bind(R.id.leader_transfer_parent)
    LinearLayout leaderTransferParent;
    @Bind(R.id.settlement_team_parent)
    LinearLayout settlementTeamParent;

    private LoadingHanlder handler;
    private CircleModel teamUser;
    private int position;

    public CircleOptionsPopup(Context context) {
        super(context);
    }

    public void hideTeamsettlement() {
        settlementTeamParent.setVisibility(View.GONE);
        leaderTransferParent.setVisibility(View.GONE);
    }

    public void hideExitTeam() {
        exitTeam.setVisibility(View.GONE);
    }

    public void setTeamUser(CircleModel teamUser, int position) {
        this.teamUser = teamUser;
        this.position = position;
    }

    @Override
    public void setUpViews() {
        handler = new LoadingHanlder(context);
    }

    @OnClick(R.id.exit_team)
    public void exitTeam(View view) {
        CircleAPI.exitCircle(teamUser.getCircleid(), this, handler);
    }

    @OnClick(R.id.leader_transfer)
    public void setLeaderTransfer(View v) {
        dismiss();
        Bundle bundle = new Bundle();
        bundle.putString("res", "MyCircleFragment");
        bundle.putString(Constants.TEAM_ID, teamUser.getCircleid());
        IntentUtils.startActivity(context, CircleMemberActivity.class, bundle);
    }

    @Override
    public void setUpListener() {

    }

    @Override
    public int getAnimationStyle() {
        return 0;
    }

    @Override
    public int getResView() {
        return R.layout.popup_circle_options;
    }

    @Override
    public void updateView(View contentView) {
        setWidth(ScreenUtils.getScreenWidth(context) * 2 / 3);
    }

    @Override
    public void start(int type) {

    }

    @Override
    public void success(int type, String json) {
        BaseModel base = JSONObject.parseObject(json, BaseModel.class);
        if (base.isSuccess()) {
            switch (type) {
                case CircleAPI.EXIT_CIECLE_CODE:
                    EventBusEntity entity = new EventBusEntity(EventBusEntity.Type.CIRCLE_EXIT);
                    entity.setItem(teamUser);
                    entity.setPosition(position);
                    EventBus.getDefault().post(entity);
                    break;
//                case :
//
//                break;
            }

        } else
            T.showShort(context, base.msg);
    }

    @Override
    public void failed(String error) {

    }

    @Override
    public void finish(int type) {

    }
}
